package listtest;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA
 * Description 自己编写 的ArrayList
 * User: 王杰
 * Date: 2025-05-16
 * Time: 9:20
 */
public class MyArrayList implements MyList{
    public int[] array;
    public int usedSize;
    public static final int DEFAULT_CAPACITY = 10;
    public MyArrayList(){
        this.array = new int[DEFAULT_CAPACITY];
    }

    @Override
    public void add(int data) {
        if (isFull()) {
            grow();
        }
        this.array[this.usedSize] = data;
        this.usedSize++;
    }

    private void grow() {
        this.array = Arrays.copyOf(this.array, 2*this.array.length);
    }

    public boolean isFull() {
        return this.usedSize == array.length;
    }


    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.println(array[i] + " ");
        }
    }
}
